var app = getApp();
var constract = require('../../../utils/constract.js');
Page({
  data: {
    
  },
  onLoad: function (options) {
    this.setData({
      options: options
    })
    if (options.state == 0) {
      wx.showModal({
        title: '您还没有名片',
        content: '创建自己的名片，让大家认识你',
        success: function (res) {
          if (res.confirm) {
            wx.redirectTo({
              url: '/pages/add-card/add-card?avatar=' + options.avatar + '&name=' + options.name
            })
          } else if (res.cancel) {
            wx.navigateBack();
          }
        }
      });
      return
    };

    var gender = options.gender;
    this.setData({
      gender: gender,
    })
    var that = this;
    app.Verification(function (token) {
      wx.request({
        url: constract.allMyImpression,
        header: { token: token },
        success: function (res) {
          console.log(res);
          console.log('-----');

          var MyImpression = res.data.data;
          console.log(MyImpression);
          that.setData({
            MyImpression: MyImpression
          });
          if (!MyImpression.length) {
            wx.request({
              url: constract.label,
              header: { token: token },
              success: function (res) {
                console.log(res);
                console.log('label↑↑↑');
                var label = res.data.data;
                for (var i = 0; i < label.length; i++) {
                  label.checked = false;
                }
                that.setData({
                  label: label,
                  gender: gender
                });
              }
            })
          }
        }
      });
    });
  },
  check: function(e){
    var that=this;
    var index=e.target.dataset.index;
    var label=this.data.label;
    var num=0;
    label[index].checked = !label[index].checked;
    that.setData({
      label:label
    });
    for (var i = 0; i < label.length; i++) {
      if (label[i].checked) {
        num++;
      }
    };
    if (num > 5) {
      wx.showModal({
        title: '提示',
        content: '最多添加五个印象',
        showCancel: false
      });
      label[index].checked = !label[index].checked;
      that.setData({
        label: label
      });
    }
  },
  skipPage: function(e){
    var name = e.currentTarget.dataset.name;
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: "../../collect/collect?type="+type+"&name="+name
    })
  },
  delImpress: function(e){
    var id = e.currentTarget.dataset.id;
    var that = this;
    var gender = this.data.options.gender;
    wx.showModal({
      title:"提示",
      content: "确认删除此印象?",
      success: function(res){
        if(res.confirm) {
          app.Verification(function (token) {
            wx.request({
              url: constract.delMyImpression,
              header: { token: token },
              data: { id: id },
              success: function (res) {
                console.log(res);
                if (res.data.code === "12000") {
                  app.Verification(function (token) {
                    wx.request({
                      url: constract.allMyImpression,
                      header: { token: token },
                      success: function (res) {
                        var MyImpression = res.data.data;
                        that.setData({
                          MyImpression: MyImpression
                        });
                        if (!MyImpression.length) {
                          wx.request({
                            url: constract.label,
                            header: { token: token },
                            success: function (res) {
                              var label = res.data.data;
                              for (var i = 0; i < label.length; i++) {
                                label.checked = false;
                              }
                              that.setData({
                                label: label,
                                gender: gender
                              });
                            }
                          })
                        }
                      }
                    });
                  });
                } else {
                  wx.showToast({
                    title: "删除失败",
                    mask: true,
                  })
                }
              }
            });
          });
        } else if(res.cancel) {
          
        }
      },
      fail: function(err){
        console.log(err)
      }
    })
  },
  save:function(){
    var label=this.data.label;
    var impression = '';
    var that = this;
    var gender = this.data.gender;

    for (var i = 0; i < label.length; i++) {
      if (label[i].checked) {
        impression += label[i].name;
        impression += ',';
      }
    }
    var impression = impression.substring(0, impression.length - 1);
    if (!impression) {
      wx.showModal({
        title: '提示',
        content: '请选择印象',
        showCancel: false
      });
      return;
    }
    if (wx.showLoading) {
      wx.showLoading({
        title: '保存中',
        mask: true,
      });
    }
    app.Verification(function (token) {
      wx.request({
        url: constract.impressionAddOrEdit,
        header: { token: token },
        data: { impression_name: impression},
        success: function (res) {
          console.log(res);
          if(+res.data.code === 14000) {
            if (wx.hideLoading) {
              wx.hideLoading();
            };

            wx.showModal({
              title:"提示",
              content: res.data.msg,
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  console.log('用户点击确定');
                  app.Verification(function (token) {
                    wx.request({
                      url: constract.allMyImpression,
                      header: { token: token },
                      success: function (res) {
                        console.log(res);
                        console.log('-----');
              
                        var MyImpression = res.data.data;
                        console.log(MyImpression);
                        that.setData({
                          MyImpression: MyImpression
                        });
                        if (!MyImpression.length) {
                          wx.request({
                            url: constract.label,
                            header: { token: token },
                            success: function (res) {
                              console.log(res);
                              console.log('label↑↑↑');
                              var label = res.data.data;
                              for (var i = 0; i < label.length; i++) {
                                label.checked = false;
                              }
                              that.setData({
                                label: label,
                                gender: gender
                              });
                            }
                          })
                        }
                      }
                    });
                  });
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            if (wx.hideLoading) {
              wx.hideLoading();
            };
            wx.navigateBack();
          }
        },
        fail: function(err) {
          console.log(err);
        }
      });
    })
  }
})